###################################################################################################
# Term Limit Individual Cohort Analysis ###########################################################
# Michael Olson and Jon Rogowski ##################################################################
###################################################################################################

# This script generates results presented in Table C.1 in the supplementary materials. 

###################################################################################################
# working directory and packages ##################################################################
###################################################################################################

  #setwd("./termlimits_polarization_analysisdata")

###################################################################################################
# load, lengthen, and clean the individual shor-mccarty data ######################################
###################################################################################################

  sm_ind <- read.csv("shor_mccarty_individual.csv")
    
  sm_ind$legprofscore <- log(sm_ind$legprofscore)

###################################################################################################
# analysis ########################################################################################
###################################################################################################  
  
# Table C.1: State Legislator Extremity and Relationship with Term Limits
    
  pooled_dem_cov <- felm(np_score~post+straddle
                          +divided_gov
                         +legprofscore+abs_diff
                         |st+year|0|st
                         ,data=sm_ind[sm_ind$party=="D",])    
  
  house_dem_cov <- felm(np_score~post+straddle
                     +divided_gov
                    +legprofscore+abs_diff
                     |st+year|0|st
                     ,data=sm_ind[sm_ind$party=="D" & sm_ind$chamber=="house",])    
  
  senate_dem_cov <- felm(np_score~post+straddle
                    +divided_gov
                    +legprofscore+abs_diff
                    |st+year|0|st
                    ,data=sm_ind[sm_ind$chamber=="senate"& sm_ind$party=="D",])  
  
  sm_ind$np_score_temp <- sm_ind$np_score
  
  pooled_rep_cov <- felm(np_score_temp~post+straddle
                         +divided_gov
                         +legprofscore+abs_diff
                         |st+year|0|st
                         ,data=sm_ind[sm_ind$party=="R",])    
  
  house_rep_cov <- felm(np_score_temp~post+straddle
                        +divided_gov
                        +legprofscore+abs_diff
                        |st+year|0|st
                        ,data=sm_ind[sm_ind$party=="R" & sm_ind$chamber=="house",])    
  
  senate_rep_cov <- felm(np_score_temp~post+straddle
                         +divided_gov
                         +legprofscore+abs_diff
                         |st+year|0|st
                         ,data=sm_ind[sm_ind$chamber=="senate"& sm_ind$party=="R",])  
  
  cohort_sg <- stargazer(pooled_dem_cov,house_dem_cov,senate_dem_cov,pooled_rep_cov,house_rep_cov,senate_rep_cov,
                        add.lines = list(c("Chamber","Pooled","House","Senate","Pooled","House","Senate"),
                                         c("State Fixed Effects","\\checkmark","\\checkmark","\\checkmark","\\checkmark","\\checkmark","\\checkmark"),
                                         c("Year Fixed Effects","\\checkmark","\\checkmark","\\checkmark","\\checkmark","\\checkmark","\\checkmark"),
                                         c("Projected $R^2$",round(summary(pooled_dem_cov)$P.r.squared,3),round(summary(house_dem_cov)$P.r.squared,3)
                                           ,round(summary(senate_dem_cov)$P.r.squared,3),round(summary(pooled_rep_cov)$P.r.squared,3)
                                           ,round(summary(house_rep_cov)$P.r.squared,3),round(summary(senate_rep_cov)$P.r.squared,3))),
                        notes.append = FALSE,notes.label = "",
                        notes="\\parbox[t]{0.965\\textwidth}{\\footnotesize \\textit{Note}: Entries are linear regression coefficients with 
            standard errors clustered on states in parentheses. Observations are at the legislator-year level. Across all models, the outcome is the unmodified Shor-McCarty Ideal Point.
           ``Served Only after Implementation'' indicates
            that a legislator was first elected after term limits were implemented. ``Served Before \\& After Implementation'' indicates that term limits
            were adopted during a legislator's career. The omitted category is legislators who served only before term limit implementation.
                        $^{**}$p$<$0.05, $^*$p$<$0.10 (two-tailed test).}",
                        omit.stat = c("rsq", "f", "ser","adj.rsq"),font.size = "footnotesize",
                        star.char=c("*","**"),star.cutoffs = c(0.10,0.05),digits=3,digits.extra=0,
                        dep.var.labels=c("Democrats","Republicans"),
                        dep.var.caption = c("Ideal Point"),table.placement = "!ht",
                        covariate.labels=c("Served Only After Implementation","Served Before \\& After Implementation",
                                           "Divided Government","ln(Leg. Professionalism)","Party Competitiveness"),
                        label="sm_cohort_analysis",
                        table.layout ="-ld-#-t-as-n",
                        title="Term Limits and Polarization: Individual Relationship with Term Limits")
  
  cat(cohort_sg, sep = '\n', file = paste(output_path,"cohort.tex",sep=""))
  
# reset working directory
  
  setwd(storewd)
 